<template>
  <!-- 采购发票详情 -->
  <div>
    <van-nav-bar :title="$route.meta.title" left-text="返回" left-arrow :fixed="true" :placeholder="true" @click-left="$router.go(-1)" :right-text="auditBtn" @click-right="editApply()" />
    <div>
      <div class="headerbox" v-if="columns.length > 1">
        <div class="header_top" :style="{ width: columns.length * 3 + 'rem' }">
          <div class="header_item" v-for="(item, index) in columns" :key="index" @click="handleClick(index)" :class="tabIndex == index ? 'active' : ''">
            <div class="txt">{{ item.number }}</div>
            <div class="txt1">{{ item.bizDate }}</div>
          </div>
        </div>
      </div>

      <div class="carInfo">
        <div class="carInfo_title">开票信息</div>
        <div class="carInfo_item">
          <span class="label_txt">采购发票单号：</span>
          {{ dataSource.billNumber }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">会计记账日期：</span>
          {{ dataSource.bizDate }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">单据状态：</span>
          {{ dataSource.billStatus }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">品牌：</span>
          {{ dataSource.brand }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">展厅：</span>
          {{ dataSource.showOrgName }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">签约公司：</span>
          {{ dataSource.signCompany }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">含税金额：</span>
          {{ dataSource.totalAmount ? dataSource.totalAmount : '-' }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">不含税金额：</span>
          {{ dataSource.noTaxAmount ? dataSource.noTaxAmount : '-' }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">增值税额：</span>
          {{ dataSource.taxAmount > -1 ? dataSource.taxAmount : '-' }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">发票客户类型：</span>
          {{ dataSource.customerType ? dataSource.customerType : '-' }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">备注：</span>
          {{ dataSource.remark ? dataSource.remark : '-' }}
        </div>
      </div>
      <div class="carInfo">
        <div class="carInfo_title">车辆信息</div>
        <div class="carInfo_item">
          <span class="label_txt">采购合同编号：</span>
          {{ true ? dataSource.purNo : '-' }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">车架号：</span>
          {{ true ? dataSource.vinCode : '-' }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt" style="width: 70px;">属性：</span>
          {{ true ? dataSource.property : '-' }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">税率：</span>
          {{ true ? dataSource.taxRate : '-' }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">折后金额：</span>
          {{ true ? dataSource.beforeDisAmt : '-' }}
        </div>
        <div class="carInfo_item">
          <span class="label_txt">折后金额不含税：</span>
          {{ true ? dataSource.beforeDisNoTaxAmt : '-' }}
        </div>
      </div>
      <table class="tablebox">
        <tr>
          <th>发票号码</th>
          <th>发票日期</th>
          <th>含税金额</th>
          <th>税率</th>
          <th>折后不含税金额</th>
          <th>增值税额</th>
        </tr>
        <tr v-for="(item, index) in dataSource.invoiceEntryList" :key="index">
          <td>{{ item.invoiceNo }}</td>
          <td>{{ item.invoiceDate }}</td>
          <td>{{ item.allAmount }}</td>
          <td>{{ item.taxRateInvoice }}</td>
          <td>{{ item.Amount }}</td>
          <td>{{ item.taxAmount }}</td>
        </tr>
      </table>

      <div class="goBack" @click="goBack">返回</div>
      <!-- 审核情况 -->
      <AuditRecord :visible="auditVisible" :billId="dataSource.billId ? dataSource.billId : detailId" @close="auditVisible = false"></AuditRecord>
    </div>
  </div>
</template>

<script>
import sellManageApi from "@/api/sellManageApi";
import AuditRecord from "@/components/AuditRecord";
export default {
  components: {
    AuditRecord
  },
  data() {
    return {
      active: "",
      detailId: "", // 当前单据ID
      orderId: "", // 当前合同订单ID
      desBosType: "", // 单据类型 desBosType
      columns: [],
      tabIndex: 0,
      dataSource: {
        billId: "", //单据Id
        billNumber: "", //单据编号
        bizDate: "", //做账日期
        billStatus: "", //单据状态
        brand: "", //所属品牌
        showOrgName: "", //所属展厅
        signCompany: "", //开票公司
        signCompanyId: "", //开票公司
        customerName: "", //客户
        carbuyer: "", //乙方买受人
        carSalePrice: "", //车辆售价含佣金
        carSaleNoTaxPrice: "", //车辆售价不含税额
        qualityPrice: "", //精品售价
        qualityNoTaxPrice: "", //精品售价不含税额
        qualityGuidePrice: "", //精品指导价
        customerType: "", //发票客户类型
        cusType: "", //车主类型
        remark: "", //备注

        apInvoiceEntry: [
          {
            entryId: "", //分录ID
            purNo: "", //销售合同号
            vinCode: "", //车架号
            property: "", //属性
            taxRate: "" //税率
          }
        ],
        invoiceInvoiceEntry: [
          {
            entryId: "", //分录ID
            invoiceNo: "", //发票号码
            invoiceDate: "", //发票日期
            allAmount: "", //含税金额
            taxRate: "", //税率
            Amount: "", //折后金额不含税
            taxAmount: "" //增值税率
          }
        ]
      },
      auditBtn: "",
      auditVisible: false
    };
  },
  mounted() {
    if (this.$route.query) {
      this.detailId = this.$route.query.billId;
      this.orderId = this.$route.query.orderId;
      this.desBosType = this.$route.query.desBosType;
      this.billNumber = this.$route.query.billNumber;
      console.log(this.detailId);
      console.log(this.orderId);
      console.log(this.desBosType);
      this.getList(this.orderId);
    }
  },
  methods: {
    editApply() {
      if (this.dataSource.billStatus == "保存") {
        if (this.dataSource.billStatus != "保存") {
          this.$toast.fail("该单据无法编辑");
          return;
        }
        this.$router.push({
          path: "/PurchaseInvoiceEdit",
          query: {
            billId: this.columns[this.tabIndex].billId,
            orderId: this.orderId,
            billNumber: this.billNumber
          }
        });
      } else {
        this.auditVisible = true;
      }
    },
    getDetail() {
      sellManageApi
        .secondHandCarFacade({
          method: "getPurchaeInvoiceById", //固定值
          billId: this.columns[this.tabIndex].billId //单据Id，必填，String
        })
        .then(res => {
          if (res.code == 1) {
            this.dataSource = res.data;
            if (this.dataSource.billStatus == "保存") {
              this.auditBtn = "编辑";
            } else {
              this.auditBtn = "审批情况";
            }
          }
        });
    },
    getList() {
      // 根据类型ID获取单据列表
      sellManageApi
        .secondHandCarFacade({
          method: "getDestInfoBySourceIdAndDestBosType", //固定值
          userId: "", //用户ID，必填，String
          orgId: "", //登录组织Id，必填，String
          billId: this.orderId, //单据Id，必填，String
          desBosType: "AAB27F21"
        })
        .then(res => {
          if (res.code == 1) {
            if (res.data.length >= 1) {
              this.columns = res.data;
              this.getDetail();
            } else {
              this.$toast.fail("暂无该单据");
            }
          } else {
            this.$toast.fail("暂无该单据");
          }
        });
    },

    handleClick(index) {
      this.tabIndex = index;
      this.getDetail();
    },
    goBack() {
      this.$router.go(-1);
    },

    fileManage() {
      this.$router.push({
        path: "/fileMange",
        query: { billId: this.dataSource.billId }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.headerbox {
  width: 100%;
  overflow-x: auto;
  &::-webkit-scrollbar {
    display: none;
  }
}
.header_top {
  display: flex;
  align-self: center;
  padding: 10px;
  overflow-x: auto;
  // width: 12rem;
  .header_item {
    width: 150px;
    background: #fff;
    padding: 10px 15px;
    font-size: 12px;
    border-radius: 5px;
    margin-right: 10px;
    .txt {
      margin-bottom: 5px;
    }
    &.active {
      border: 1px solid #ccc;
      box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);
    }
  }
}
.carInfo {
  padding: 10px;
  background: #fff;
  margin-bottom: 10px;
  .source {
    padding-left: 0 !important;
    margin-bottom: 10px;
    justify-content: space-between;

    .source_txt {
      font-size: 14px;
    }
  }

  .carInfo_No {
    margin-bottom: 15px;
    .carInfo_No_item {
      border-right: 1px solid #ccc;
      padding-right: 5px;
      margin-right: 5px;
      font-size: 12px;
      color: #333;
      &:last-child {
        border: none;
      }
    }
  }
  .carInfo_item {
    font-size: 14px;
    margin-bottom: 5px;
    display: flex;
    color: #333;
    .carInfo_item_price {
      width: 50%;
      display: flex;
    }
    .label_txt {
      color: #666;
      display: block;
    }
  }
  .carInfo_title {
    font-size: 14px;
    color: #333;
    margin-bottom: 10px;
    font-weight: bold;
  }
}
.goBack {
  width: 80%;
  background: #2381e6;
  color: #fff;
  font-size: 14px;
  margin: auto;
  border-radius: 5px;
  padding: 10px;
  text-align: center;
  margin-bottom: 25px;
}
.tablebox {
  width: 95%;
  margin: auto;
  border-collapse: collapse;
  text-align: center;
  background: #fff;
  margin-bottom: 25px;

  tr {
    th {
      font-size: 12px;
      border: 1px solid #ccc;
      padding: 10px 0;
      background: #f1f1f1;
    }
    td {
      padding: 5px 0;
      border: 1px solid #ccc;
      font-size: 12px;
    }
  }
}
</style>
